var pmd = require('../../template/roll/roll.js');
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    swiperinfo: {
      hgt: 0,
      indicatorDots: true,
      autoplay: false,

      circular: true,
      interval: 5000,
      duration: 500,
    },
    showModel: false,
    banners: [],
    pmd: "0",
    swiperCurrent: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        var swiper_hgt = 564 * res.windowWidth / 1000;
        var swiperinfo = that.data["swiperinfo"];
        swiperinfo.hgt = swiper_hgt;
        that.setData({
          swiperinfo: swiperinfo,
          win_width: res.windowWidth
        });
      },
    });
    this.loadBanners();
    this.loadNotice();
  },
  checkLogin: function () {
    var that = this;
    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        app.globalData.code = res.code;
        // console.log(res.code);
        that.checkToken();
      },
      fail: function () {
        wx.showToast({
          title: '系统提示:网络错误',
          icon: 'warn',
          duration: 1500,
        })
      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (this.data.pmd == '1') {
      pmd.run1();
    }
    this.loadIndex();
    this.checkToken();
  },
  testCallBack: function (res) {

  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    pmd.stopRun1();
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    // console.log("bottom");
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  agreeGetUser: function (e) {
    console.log(e);
    //设置用户信息本地存储
    try {
      wx.setStorageSync('userInfo', e.detail.userInfo);
    }
    catch (e) {
      wx.showToast({
        title: '系统提示:网络错误',
        icon: 'warn'
      });
    }
    let that = this;
    that.setData({
      showModel: false
    });
    app.getOP(e.detail.userInfo);
  },
  loadIndex: function () {
    var goodsinfos = [
      {
        name: '法式餐包',
        goodsid: 1,
        price: '8',
        url: '../imgs/index/demo4.jpg'
      },
      {
        name: '马卡龙蛋糕',
        goodsid: 2,
        price: '15',
        url: '../imgs/index/demo5.jpg'
      },
      {
        name: '防弹咖啡',
        goodsid: 3,
        price: '15',
        url: '../imgs/index/demo4.jpg'
      },
      {
        name: '布朗尼',
        goodsid: 4,
        price: '298',
        url: '../imgs/index/demo5.jpg'
      }
    ];
    this.setData({
      goodsinfos: goodsinfos
    });
  },
  loadIndexCallBack: function (res) {
    wx.hideToast();
    if (res.status != 'ok') {
      wx.showToast({
        title: '网络错误~',
        image: '../imgs/tips.png'
      });
      return;
    }
  },
  checkToken: function () {
    return;
    var loginCode = app.globalData.code;
    if (loginCode == '' || loginCode == undefined) {
      this.checkLogin();
      return;
    }
    var url = '/weixin_wheat/open-api';//
    var params = {
      type: "getOpenidByCodeWeApp",
      uname: "WeApp",
      param: app.globalData.code
    }
    app.loadDataFromServer(url, this.testCallBack, params, 'post')
  },
  toGoodsDetail: function (event) {
    var goodsid = event.currentTarget.dataset.goodsid;
    wx.navigateTo({
      url: '../store/goodsdetail/goodsdetail?goodsid=' + goodsid,
    });
  },
  swiperChange: function (e) {
    if (e.detail.source == "touch") {
      //防止swiper控件卡死  
      if (this.data.current == 0) {// 卡死时，重置current为正确索引
        this.setData({
          swiperCurrent: 0
        });
      }
      else {//正常轮转时，记录正确页码索引  
        this.setData({
          swiperCurrent: e.detail.current
        });
      }
    }
  },
  dotTap: function (e) {
    var index = e.currentTarget.id;
    this.setData({
      swiperCurrent: index
    });
  },
  paomadeng: function () {
    pmd.run1();
  },
  // ------------------------加载公告-----------------------
  loadNotice: function () {
    var that = this;
    if (app.globalData.address == null) {
      app.getAddress();
      var timeid = setTimeout(function () {
        clearTimeout(timeid);
        that.loadNotice();
      }, 100);
      return;
    }
    var url = '/yuanmai_we/activity/mesnotice/get/';
    url = url + app.globalData.address.CITYID;
    app.loadDataFromServer(url, this.loadNoticeCallBack, {}, 'get', true);
  },
  loadNoticeCallBack: function (res) {
    // if(res.length <= 0){
    //   return;
    // }
    this.setData({
      pmd: '1'
    });
    pmd.rollInit(this);
    this.paomadeng();
  },
  //---------------------------加载banner-------------------------
  loadBanners: function () {
    var that = this;
    if (app.globalData.address == null) {
      app.getAddress();
      var timeid = setTimeout(function () {
        clearTimeout(timeid);
        that.loadBanners();
      }, 100);
      return;
    }
    var url = '/yuanmai_we/activity/banner/get/';
    url = url + app.globalData.address.CITYID;
    app.loadDataFromServer(url, this.loadBannersCallBack, {}, 'get', true);
  },
  loadBannersCallBack: function (res) {
    // console.log(res);
    var banners = [];
    for (var i = 0; i < res.length; i++) {
      var bannerinfo = {
        imgurl: res[i].picurl,
        name: res[i].bannername
      };
      banners.push(bannerinfo);
    }
    this.setData({
      banners: banners
    });
  },
  bargainAction: function () {
    wx.navigateTo({
      url: '../bargain/bargainindex',
    });
  },
  //-----------------------------------------------------------
  showNotice: pmd.showNotice,
  closeNotice: pmd.closeNotice
})